package com.atguigu.java8.lambda;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;

public class TestLambda {

    // 原来的匿名内部类
    @Test
    public void test1() {
        Comparator<Integer> comparator = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1, o2);
            }
        };
        TreeSet<Integer> treeSet = new TreeSet<>(comparator);
    }

    // lambda表达式
    @Test
    public void test2() {
        Comparator<Integer> comparator = (x, y) -> Integer.compare(x, y);
        TreeSet<Integer> treeSet = new TreeSet<>(comparator);
    }

    List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99, Employee.Status.FREE),
            new Employee("李四", 38, 5555.99, Employee.Status.BUSY),
            new Employee("王五", 50, 6666.66, Employee.Status.VOCATION),
            new Employee("赵六", 16, 3333.33, Employee.Status.FREE),
            new Employee("田七", 8, 7777.77, Employee.Status.BUSY)
    );

    // 需求：获取当前公司中员工年龄大于35的员工信息
    @Test
    public void test3() {
        List<Employee> employeeList = filterEmployees(this.employees);
        for (Employee employee : employeeList) {
            System.out.println(employee);
        }
        System.out.println("-----------------------------------");

        List<Employee> highSalaryEmps = filterEmployees2(this.employees);
        for (Employee highSalaryEmp : highSalaryEmps) {
            System.out.println(highSalaryEmp);
        }
    }

    public List<Employee> filterEmployees(List<Employee> employees) {
        List<Employee> emps = new ArrayList<>();

        for (Employee employee : employees) {
            if (employee.getAge() >= 35) {
                emps.add(employee);
            }
        }
        return emps;
    }

    // 需求：获取当前公司中员工工资大于5000的员工信息
    public List<Employee> filterEmployees2(List<Employee> employees) {
        List<Employee> emps = new ArrayList<>();

        for (Employee employee : employees) {
            if (employee.getSalary() >= 5000.0) {
                emps.add(employee);
            }
        }
        return emps;
    }

    // 优化方式一：策略设计模式
    @Test
    public void test4() {
        List<Employee> employeeList = filterEmployees(this.employees, new FilterEmployeeByAge());
        for (Employee employee : employeeList) {
            System.out.println(employee);
        }
        System.out.println("-----------------------------------");

        List<Employee> highSalaryEmps = filterEmployees(this.employees, new FilterEmployeeBySalary());
        for (Employee emp : highSalaryEmps) {
            System.out.println(emp);
        }
    }

    public List<Employee> filterEmployees(List<Employee> employees, MyPredicate<Employee> myPredicate) {
        List<Employee> emps = new ArrayList<>();

        for (Employee employee : employees) {
            if (myPredicate.test(employee)) {
                emps.add(employee);
            }
        }
        return emps;
    }

    // 优化方式二：匿名内部类
    @Test
    public void test5() {
        List<Employee> employeeList = filterEmployees(this.employees, new MyPredicate<Employee>() {
            @Override
            public boolean test(Employee employee) {
                return employee.getAge() >= 35;
            }
        });
        for (Employee employee : employeeList) {
            System.out.println(employee);
        }
    }

    // 优化方式三：Lambda表达式
    @Test
    public void test6() {
        List<Employee> employees = filterEmployees(this.employees, e -> e.getSalary() >= 5000);
        employees.forEach(System.out::println);
    }

    // 优化方式四：
    @Test
    public void test7() {
        employees.stream().filter(e -> e.getSalary() > 5000)
                .limit(2)
                .forEach(System.out::println);

        System.out.println("--------------------------------");

        employees.stream().map(Employee::getName).forEach(System.out::println);
    }
}
